package com.example.demo.lc.array;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* @author wzh
* @date 2021/7/20 9:00 下午
* @Description   lc-1
 * 数组
 * https://leetcode-cn.com/problems/two-sum/
*/
public class LeetCode1 {
    public static void main(String[] args) {
        //int[]  nums = {2,7,11,15};int target = 9;
        //int[]  nums = {3,2,4};int target = 6;
        int[]  nums = {3,3};int target = 6;
        System.out.println(Arrays.toString(twoSum(nums,target)));
    }

    public static int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(!map.containsKey(nums[i])){
                map.put(target-nums[i],i);
            }else{
                return new int[]{i,map.get(nums[i])};
            }
        }
        return new int[0];
    }

}
